package  com.lenovo.android.syncacc;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import com.lenovo.android.syncacc.R;
import com.lenovo.android.syncacc.controller.MyAndroidController;

public class MyAppInitializer {
	private String TAG = "MyAppInitializer";
	private Context context;

	public MyAppInitializer(Context context) {

		this.context = context;
	}

	
	public void initAccount(Activity activity) {

		Account account = MyAndroidController.getNativeAccount();

		if (account == null) {
			Log.d(TAG, "Account not found, now start create a default one");

			// Create the account through our account authenticator
			AccountManager am = AccountManager.get(context);
			am.addAccount(context.getString(R.string.account_type), null, null,
					null, activity, new AccountManagerCallback<Bundle>() {
						public void run(AccountManagerFuture<Bundle> result) {
							try {
								// Get the authenticator result, it is blocking
								// until the
								// account authenticator completes
								result.getResult();
								Log.d(TAG, "@@@@@@@@@@@@@@@@@@@@@@Account created");
							} catch (Exception e) {
								Log.d(TAG,
										"Exception during account creation: ",
										e);
							}
						}
					}, null);
		} else {
			Log.d(TAG, "Account already defined");
		}

	}

}
